//
//  KABCosDetailTCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/9.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosDetailTCell.h"
#import "KABModel.h"
#import <YYWebImage.h>
#import "NSString+XDQiniu.h"

@implementation KABCosDetailTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cosImgViewTapped:)];
    [self.cosImgView addGestureRecognizer:tapGesture];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)cosImgViewTapped:(UIGestureRecognizer *)gesture {
    if ([self.delegate respondsToSelector:@selector(kABCosDetailTCell:imgViewTapped:)]) {
        [self.delegate kABCosDetailTCell:self imgViewTapped:(UIImageView *)gesture.view];
    }
}

- (void)configViewWithCosDetPubItem:(KABCosDetPubItem *)item loadImage:(BOOL)loadImage {
    self.desLabel.text = item.content;
    if (!item.content.length) {
        self.desLabelTopConstraint.constant = 3;
        self.desLabelBottomContraint.constant = 3;
    } else {
        self.desLabelTopConstraint.constant = 8;
        self.desLabelBottomContraint.constant = 8;
    }
    
//    if (loadImage) {
//    }
    self.cosImgViewHeightConstraint.constant = 375; // 防止图片为空时，不执行completion的Block
    NSString *thumbImgUrl = [item.image_url xd_qiniuImgUrlWithMaxWidth:CGRectGetWidth([UIScreen mainScreen].bounds)];
    NSLog(@"cos详情的缩略图url = %@", thumbImgUrl);
    [self.cosImgView yy_setImageWithURL:[NSURL URLWithString:thumbImgUrl]
                            placeholder:[KABDefaultVarModel sharedInstance].bigDefaultImg
                                options:kNilOptions
                             completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                                 if (image) {
                                     self.cosImgViewHeightConstraint.constant = floor([UIScreen mainScreen].bounds.size.width * image.size.height /image.size.width);
                                     [self imageViewDownLoad];
                                 }
                             }];
}

- (void)imageViewDownLoad {
    if ([self.delegate respondsToSelector:@selector(kABCosDetailTCell:imgViewDownload:)]) {
        [self.delegate kABCosDetailTCell:self imgViewDownload:self.cosImgView];
    }
}

@end


@implementation KABCosDetailTCell (MallProductDetail)

- (void)configViewWithProDetImgItem:(KABProDetailImgItem *)item {
    self.desLabel.text = nil;
    self.desLabelTopConstraint.constant = 3;
    self.desLabelBottomContraint.constant = 3;
    
    //    if (loadImage) {
    //    }
    self.cosImgViewHeightConstraint.constant = 375; // 防止图片为空时，不执行completion的Block
    [self.cosImgView yy_setImageWithURL:[NSURL URLWithString:item.image_url]
                            placeholder:[KABDefaultVarModel sharedInstance].bigDefaultImg
                                options:kNilOptions
                             completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                                 if (image) {
                                     self.cosImgViewHeightConstraint.constant = floor([UIScreen mainScreen].bounds.size.width * image.size.height /image.size.width);
                                     [self imageViewDownLoad];
                                 }
                             }];
}

@end
